﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace SB.Tools
{
    public class SortableBindingList<T> : BindingList<T>
    {
        private bool _isSorted;

        protected override bool SupportsSortingCore
        {
            get
            {
                return true;
            }
        }

        protected override void ApplySortCore(
        PropertyDescriptor property, ListSortDirection
        direction)
        {
            List<T> items = this.Items as List<T>;
            if (items != null)
            {
                PropertyComparer<T> pc =
                new PropertyComparer<T>(property,
                direction);

                items.Sort(pc);
                _isSorted = true;
            }
            else
            {
                _isSorted = false;
            }

            this.OnListChanged(
            new ListChangedEventArgs(ListChangedType.Reset,
            -1));
        }

        protected override bool IsSortedCore
        {
            get { return _isSorted; }
        }
    }
}
